package com.guodi.map.controller;

import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapMetadata;
import com.guodi.map.service.IMapMetadataService;
import com.guodi.map.vo.ApiMapMetadata;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;


/**
 * @描述：地图图层元数据表控制类
 * @作者: 代鹏刚
 * @日期: 2019-12-27
 */
@Validated
@RestController
@RequestMapping("/MapMetadata")
@Api(value = "API - MapMetadataController", tags = "地图图层元数据表")
public class MapMetadataController extends BaseController {

    @Autowired
    public IMapMetadataService mapMetadataService;

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-27
     **/
    @PostMapping("/findByLayerId")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "layerId", value = "ID", required = true, dataType = "string", paramType = "query")
    })
    public R findByLayerId(String layerId) {
        if(StringUtils.isBlank(layerId)){
            return R.fail(StatusCode.ERROR_CODE_40005,"layerId不能为空");
        }
        MapMetadata mapMetadata = mapMetadataService.findByLayerId(layerId);
        return R.data(mapMetadata);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-27
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@Valid ApiMapMetadata apiMapMetadata){
        MapMetadata mapMetadata = new MapMetadata();
        BeanUtils.copyProperties(apiMapMetadata,mapMetadata);
        prepareData(mapMetadata, true);
        mapMetadata.setId(UuidUtil.get32UUID());
        mapMetadataService.save(mapMetadata);
        return R.data(mapMetadata);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-27
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapMetadata mapMetadata) {
        prepareData(mapMetadata, false);
        mapMetadataService.updateById(mapMetadata);
        return R.data(mapMetadata);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-27
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "query")
    })
    public R delete(String id){
        return R.status(mapMetadataService.removeById(id));
    }

    private void prepareData(MapMetadata mapMetadata, boolean isSave) {
        if(isSave){
            mapMetadata.setCreator(getUser().getUserId().toString());
            mapMetadata.setCreateTime(new Date());
            //防止误修改
            mapMetadata.setModifier(null);
            mapMetadata.setModifyTime(null);
        }else{
            mapMetadata.setModifier(getUser().getUserId().toString());
            mapMetadata.setModifyTime(new Date());
            //防止误修改
            mapMetadata.setCreator(null);
            mapMetadata.setCreateTime(null);
        }
    }
}
